package com.lin.manager.notice;

import cn.hutool.core.collection.CollectionUtil;
import com.lin.manager.common.enums.NoticeEnum;
import com.lin.manager.common.ex.BizException;
import com.lin.manager.notice.message.NoticeMessage;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @作者: 林江
 * @创建时间: 2024/4/11
 * @功能:
 */
@Component
public class NoticeMessageHelper implements InitializingBean {

    @Resource
    private List<NoticeMessage> noticeMessages;

    private Map<NoticeEnum, NoticeMessage> map = new HashMap<>();


    public NoticeMessage get(Integer sendType) {
        NoticeEnum type = NoticeEnum.get(sendType);
        if (Objects.isNull(type)) {
            throw new BizException("系统不支持其他方式发送");
        }

        NoticeMessage noticeMessage = map.get(type);
        if (Objects.isNull(noticeMessage)) {
            throw new BizException("系统不支持其他方式发送");
        }

        return noticeMessage;
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        if (CollectionUtil.isEmpty(noticeMessages)) {
            return;
        }

        for (NoticeMessage noticeMessage : noticeMessages) {
            map.put(noticeMessage.getSendType(), noticeMessage);
        }
    }
}
